import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    // 推荐音乐
    path: "/",
    component: () => import("@/views/parent/HomeRecommend.vue"),
    meta: {
      nav: true,
      title: "推荐音乐",
    },
  },
  {
    // 歌手
    path: "/singer",
    component: () => import("@/views/parent/HomeSinger.vue"),
    meta: {
      nav: true,
      title: "歌手",
    },
  },
  {
    // 搜索
    path: "/search",
    component: () => import("@/views/parent/HomeSearch.vue"),
    meta: {
      nav: true,
      title: "搜索",
    },
  },
  {
    // 我的
    path: "/my",
    component: () => import("@/views/parent/HomeMy.vue"),
    meta: {
      nav: true,
      title: "我的",
    },
  },
  {
    // 歌单详情
    path: "/playlist",
    component: () => import("@/views/child/Playlist.vue"),
    meta: {
      nav: false,
      title: "歌单详情",
    },
  },
  {
    // 歌单介绍详情
    path: "/playlist_detail",
    component: () => import("@/views/child/PlaylistDetail.vue"),
    meta: {
      nav: false,
      title: "歌单介绍详情",
    },
  },
  {
    // 歌单评论页
    path: "/playlist_comments",
    component: () => import("@/views/child/PlayComments.vue"),
    meta: {
      nav: false,
      title: "歌单评论页",
    },
  },
  {
    // 歌手详情页
    path: "/singer_detail",
    component: () => import("@/views/child/SingerDetail.vue"),
    meta: {
      nav: false,
      title: "歌手详情页",
    },
  },
  {
    // 专辑详情页
    path: "/album_detail",
    component: () => import("@/views/child/AlbumDetail.vue"),
    meta: {
      nav: false,
      title: "专辑详情页",
    },
  },
  {
    // 视频详情页
    path: "/video_detail",
    component: () => import("@/views/child/VideoDetail.vue"),
    meta: {
      nav: false,
      title: "视频详情页",
    },
  },
];

const router = new VueRouter({ routes });

router.beforeEach((to, from, next) => {
  from;
  document.title = to.meta.title;
  next();
});

// 解决路由报错的bug
// 保存原始的push和replace方法
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;

// 重写push
VueRouter.prototype.push = function push(location) {
  return originPush.call(this, location).catch((err) => err);
};

// 重写replace
VueRouter.prototype.replace = function replace(location) {
  return originReplace.call(this, location).catch((err) => err);
};

export default router;
